package cn.tworice.admin.web;

import cn.tworice.admin.service.CompaniesService;
import cn.tworice.common.entity.Companies;
import cn.tworice.common.entity.CompaniesType;
import cn.tworice.common.entity.dto.CompaniesDto;
import com.lmk.core.web.support.api.RequestResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业管理控制层
 *
 * @Author 二饭
 * @Email 1786705138@qq.com
 * @Date 2021/12/9 18:54
 */
@RestController
@RequestMapping("admin/companies")
@Slf4j
@CrossOrigin
public class CompaniesController {

    @Autowired
    private CompaniesService companiesService;

    @GetMapping("list")
    @ApiOperation("获取企业信息列表")
    public RequestResult list(Integer pageSize, Integer page,Integer type) {
        RequestResult result = new RequestResult();
        List<CompaniesDto> list = companiesService.getList(page, pageSize,type);
        result.appendData("list", list);
        result.appendData("total",companiesService.getCompaniesTotal(type));
        return result;
    }
    @GetMapping("typeList")
    @ApiOperation("获取企业信息列表")
    public RequestResult typeList() {
        RequestResult result = new RequestResult();
        List<CompaniesType> list = companiesService.getTypeList();
        result.appendData("list", list);

        return result;
    }

    @PostMapping("edit")
    @ApiOperation("编辑或添加企业信息")
    public RequestResult edit(Companies companies, Integer pageSize, Integer page,Integer type) {
        RequestResult result = new RequestResult();
        companiesService.setCompanies(companies);
        result.appendData("list", companiesService.getList(page, pageSize,type));
        result.appendData("total",companiesService.getCompaniesTotal(type));
        return result;
    }

    @DeleteMapping("del")
    @ApiOperation("删除企业信息")
    public RequestResult del(String id,Integer pageSize, Integer page,Integer type) {
        RequestResult result = new RequestResult();
        companiesService.delCompanies(id);
        result.appendData("list", companiesService.getList(page, pageSize,type));
        result.appendData("total",companiesService.getCompaniesTotal(type));
        return result;
    }
}
